<template>
  <el-card shadow="always">
    <div slot="header">院系管理</div>
    <!-- card body -->
    <div class="btns">
      <div>
        <el-input
          placeholder="院系名称"
          size="mini"
          clearable
          v-model="params.query"
          @clear="resetQuery"
        ></el-input>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="resetQuery"
          >查询</el-button
        >
      </div>
      <el-button
        type="success"
        size="mini"
        icon="el-icon-plus"
        v-if="(getBtnAutMixin & 2) == 2"
        @click="addDialog = true"
        >新增</el-button
      >
    </div>
    <!--
      通过ElementUI 组件的 height 属性设置表格高度为 任意值，激活滚动条
      通过弹性盒子规则，将表格内容的高度设置为填充剩余高度
    -->
    <el-table :data="list" border size="mini" height="400">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="院系名称" prop="department_name"></el-table-column>
      <el-table-column label="专业数量" prop="major_count"></el-table-column>
      <el-table-column label="教师数量" prop="teacher_count"></el-table-column>
      <el-table-column label="操作" width="160">
        <template #default="{ row }">
          <el-button type="text" size="small">编辑</el-button>
          <PopconfirmButton
            :popconfirm="{ title: `确认删除 “${row.department_name}” 院系?` }"
            :button="{ type: 'text', size: 'small' }"
            @confirm="removeDepartment(row.department_id)"
            >删除</PopconfirmButton
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="total"
      :current-page="params.pageNo"
      :page-size="params.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="changePage"
      @size-change="changeSize"
    ></el-pagination>

    <AddDepartmentDialog :visible.sync="addDialog" @reload="findDepartList" />
  </el-card>
</template>

<script>
import { selectDepartmentList, deleteDepartmentById } from "@/api/department";
import PopconfirmButton from "@/components/PopconfirmButton.vue";
import AddDepartmentDialog from "./dialog/AddDepartmentDialog.vue";
export default {
  components: {
    PopconfirmButton,
    AddDepartmentDialog,
  },
  data() {
    return {
      total: 0,
      list: [],
      addDialog: false,
      params: {
        pageNo: 1,
        pageSize: 10,
        query: "",
      },
    };
  },
  methods: {
    async findDepartList() {
      let { data } = await selectDepartmentList(this.params);
      this.list = data.list || [];
      this.total = data.total || 0;
    },
    resetQuery() {
      this.params.pageNo = 1;
      this.findDepartList();
    },
    changeSize(pageSize) {
      this.params.pageNo = 1;
      this.params.pageSize = pageSize;
      this.findDepartList();
    },
    changePage(pageNo) {
      this.params.pageNo = pageNo;
      this.findDepartList();
    },
    async removeDepartment(departmentId) {
      await deleteDepartmentById({ department_id: departmentId });
      this.findDepartList();
    },
  },
  created() {
    this.findDepartList();
  },
};
</script>
